package cn.fintecher.gis.service;

import cn.fintecher.gis.entity.BusinessLayer;
import cn.fintecher.gis.enums.CommonShow;
import cn.fintecher.gis.enums.FlowType;
import cn.fintecher.gis.enums.Pellucidity;
import cn.fintecher.gis.exception.BadRequestException;
import cn.fintecher.gis.geoserver.GeoManagerService;
import cn.fintecher.gis.model.ParsingShpResults;
import cn.fintecher.gis.model.request.BusinessLayerRequest;
import cn.fintecher.gis.model.session.SessionModel;
import cn.fintecher.gis.postgis.PostGISService;
import cn.fintecher.gis.respository.BusinessLayerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * @Author WangZhao
 * @CreateDate 2018/12/24 14:19
 */
@Service("businessLayerService")
public class BusinessLayerService {
    private final Logger logger = LoggerFactory.getLogger(BusinessLayerService.class);

    @Autowired
    private BusinessLayerRepository businessLayerRepository;

    @Autowired
    private PostGISService postGISService;

    @Autowired
    private GeoManagerService geoManagerService;

    private final String workSpace = "business-space";


    /**
     * 添加业务图层信息（集合）
     * @param sessionModel
     * @return
     * @throws Exception
     */
    public List<BusinessLayer> createBusinessLayer(List<BusinessLayerRequest> requestList, SessionModel sessionModel, FlowType type) throws Exception {
        List<BusinessLayer> layers = new ArrayList<>();
        for (BusinessLayerRequest request : requestList) {
            BusinessLayer businessLayer = new BusinessLayer();
            businessLayer.setType(type);
            if (request.getFileId() != null && !request.getFileId().trim().equals("")) {
                ParsingShpResults result1 = postGISService.uploadShp(request.getFileId());
                if (!result1.getIsSuccess()) {
                    throw new BadRequestException("importShp", result1.getMessage());
                }
                ParsingShpResults result2 = geoManagerService.releaseLayer(request.getFileId(), result1.getCrs(), request.getStyleNames(), workSpace);
                if (!result2.getIsSuccess()) {
                    throw new BadRequestException( "importShp", result2.getMessage());
                }
//                geoManagerService.changeLayerStyles(request.getFileId(), request.getStyleNames(), layerSpace);
                businessLayer.setLayerCode(request.getFileId());//图层编号
                businessLayer.setGroupId(request.getGroupId());//图层组id
                businessLayer.setTableName(request.getFileId());//表名称
                businessLayer.setLayerName(request.getLayerName());//图层名称
                businessLayer.setLayerSpace(workSpace);//空间编号
            }

            businessLayer.setLayerShow(CommonShow.NO);//默认不展示
            businessLayer.setCreateDate(new Date());//创建时间
            businessLayer.setOpacity(Pellucidity.FALSE);//非透明
            businessLayer.setCreateOperator(sessionModel.getId());//创建人id
            businessLayer.setCreateName(sessionModel.getUsername());//创建姓名
            BusinessLayer business = businessLayerRepository.save(businessLayer);
            layers.add(business);
        }
        return layers;
    }

    /**
     * 添加添加业务图层信息(单个)
     *
     * @param groupId
     * @param fileId
     * @param layerName
     * @param sessionModel
     * @param styleNames
     * @return
     * @throws Exception
     */
    public BusinessLayer createBusinessLayer(String groupId, String fileId, String layerName,
                                             List<String> styleNames, FlowType type,
                                             SessionModel sessionModel) throws Exception {
        BusinessLayer businessLayer = new BusinessLayer();
        businessLayer.setType(type);
        if (fileId != null && !fileId.trim().equals("")) {
            ParsingShpResults result1 = postGISService.uploadShp(fileId);
            if (!result1.getIsSuccess()) {
                throw new BadRequestException( "importShp", result1.getMessage());
            }
            ParsingShpResults result2 = geoManagerService.releaseLayer(fileId, result1.getCrs(), styleNames, workSpace);
            if (!result2.getIsSuccess()) {
                throw new BadRequestException("importShp", result2.getMessage());
            }
//            geoManagerService.changeLayerStyles(fileId, styleNames, layerSpace);
            businessLayer.setLayerCode(fileId);//图层编号
            businessLayer.setTableName(fileId);//表名称
            businessLayer.setLayerName(layerName);//图层名称
            businessLayer.setLayerSpace(workSpace);//空间编号
        }
        businessLayer.setGroupId(groupId);//图层组id
        businessLayer.setLayerShow(CommonShow.NO);//默认不展示
        businessLayer.setCreateDate(new Date());//创建时间
        businessLayer.setCreateOperator(sessionModel.getId());//创建人id
        businessLayer.setCreateName(sessionModel.getUsername());//创建姓名
        businessLayer.setOpacity(Pellucidity.FALSE);//非透明
        BusinessLayer business = businessLayerRepository.save(businessLayer);
        return business;
    }

}
